#!/bin/sh -
#
# $Id$
#
# Check to make sure we can run DB 1.85 code.
d=../../
b=./tmp_build/
s=$d/src

mkdir -p $b

[ -f $d/LICENSE ] || {
	echo 'FAIL: Test must be run from scr directory.'
	exit 1
}

nocleanup=0
while [ $# -gt 0 ]
do 
	case "$1" in
	-nocleanup)
		nocleanup=1; shift;;
	*)
		echo "Unrecognized option: $1, ignoring"
		shift;;
	esac
done

opts="--enable-compat185 --disable-shared"
echo "Building DB library, this can take a while."
(cd $b && ../../../dist/configure $opts > /dev/null && make libdb.a > /dev/null) || {
	echo 'FAIL: unable to build libdb.a'
	exit 1
}

# if compiling on linux blade server, add -pthread on cc
CINC="-I$b -I$s -I$s/dbinc"
[ `uname` = "Linux" ] && CINC="$CINC -pthread"

for i in `ls test_*.c`; do

	echo "=== Running $i ===" | tee -a compile.out
	if cc -g -Wall $CINC $i $b/libdb.a -o t >> compile.out 2>&1; then
		:
	else
		echo "FAIL: unable to compile test program $i"
		exit 1
	fi

	if ./t; then
		:
	else
		echo "FAIL: test program failed"
		exit 1
	fi
	rm -f ./t
done

# Cleanup.
# TODO: The test should be consistent, so this cleanup isn't so haphazard.
#       Alternatively we could build each test in a sub-dir and cleanup after
#       individual runs.
rm a.db __db.* output
rm -rf ./TESTDIR

if [ nocleanup = 0 ]; then
	rm -rf compile.out $b
fi

exit 0
